package com.group4.DB;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.group4.DAO.PageDAO;

public class Page {

	private int ID;
	private int parentId;
	private String pageName;
	private String pageUrl;
	private int roleId;

	static PreparedStatement ps = null;
	static ResultSet rs = null;
	
	public static PageDAO getPageWithID(int ID) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		PageDAO page = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_Page where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				page = new PageDAO();

				page.setID(ID);
				
				page.setParentId(rs.getInt("parent_id"));
				page.setPageName(rs.getString("page_name"));
				page.setPageUrl(rs.getString("page_url"));
				page.setRoleId(rs.getInt("page_roleId"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getPageWithID.");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getPageWithID. ");
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getPageWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in getPageWithID. ");
					e.printStackTrace();
				}
			}
		}
		return page;
	}

	public boolean addPage(PageDAO page) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_Page (ID, parent_id, page_name, page_url, page_roleId)"
							+ " values (?, ?, ?, ?, ?)");

			ps.setInt(1, page.getID());
			ps.setInt(2, page.getParentId());
			ps.setString(3, page.getPageName());
			ps.setString(4, page.getPageUrl());
			ps.setInt(5, page.getRoleId());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addPage. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addPage. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addPage.");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public boolean updatePage(PageDAO page) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			PageDAO oldPage = getPageWithID(page.getID());

			if (oldPage == null) {
				System.out.println("No Page found with ID = " + page.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_Page set parent_id = ?, page_name = ?, page_url = ?, page_roleId = ? where ID = ?");

			ps.setInt(1, page.getParentId());
			ps.setString(2, page.getPageName());
			ps.setString(3, page.getPageUrl());
			ps.setInt(4, page.getRoleId());
			ps.setInt(5, page.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updatePage. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updatePage. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in addPage. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deletePage(int ID) {
		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		Page Page = null;

		try {
			PageDAO oldPage = getPageWithID(ID);

			if (oldPage == null) {
				System.out.println("No Page found with ID = " + ID);
				return result;
			}

			ps = ConnectionManager.conn.prepareStatement("delete from tbl_Page where ID = ?");

			ps.setInt(1, ID);

			ps.execute();

			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deletePage. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deletePage. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in deletePage. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Page [ID:").append(ID);
		sb.append("; parent_id:").append(parentId);
		sb.append("; page_name:").append(pageName);
		sb.append("; page_url:").append(pageUrl);
		sb.append("; page_roleId:").append(roleId);
		sb.append("]");

		return sb.toString();
	}

}
